package top.wardyan.wdev.dbassist.action.dic;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import top.wardyan.action.base.AbstractShell;
import top.wardyan.control.WStatusCombo;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbassist.config.DDA;
import top.wardyan.wdev.dbassist.model.YTDic;

public class DicAddShell extends AbstractShell<DicAddShell>{
	protected DDA dda = DDA.getInstance();
	//private IYPGetIDService getIDService = dda.getIDService;
	//private IMaster
	protected Label lblTop;
	protected Label lblSID;
	protected Text txtSID;
	protected Label lblName;
	protected Text txtXName;
	protected Label lblField;
	protected Text txtXField;
	protected Label lblField2;
	protected Text txtXField2;
	protected Label lblFIeld3;
	protected Text txtXField3;
	protected Label lblNote;
	protected Text txtXNote;
	protected Label lblStatus;
	protected CCombo cmbStatus;
	protected Button btnSave;
	protected Button btnReset;

	public DicAddShell() {
		super(SWT.CLOSE);
		this.addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent arg0) {
				DicShell parent = (DicShell)self.getParent();
				//parent.list();
			}
			
		});
		lblTop = new Label(this, SWT.NONE);
		lblTop.setFont(SWTResourceManager.getFont("宋体", 36, SWT.NORMAL));
		lblTop.setAlignment(SWT.CENTER);
		lblTop.setBounds(0, 10, 798, 60);
		lblTop.setText("新增");
		
		lblSID = new Label(this, SWT.NONE);
		lblSID.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblSID.setAlignment(SWT.RIGHT);
		lblSID.setBounds(272, 135, 80, 22);
		lblSID.setText("sID*");
		
		txtSID = new Text(this, SWT.BORDER);
		txtSID.setTextLimit(9);
		txtSID.setBounds(358, 131, 200, 30);
		
		lblName = new Label(this, SWT.NONE);
		lblName.setText("名称*");
		lblName.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblName.setAlignment(SWT.RIGHT);
		lblName.setBounds(272, 171, 80, 22);
		
		txtXName = new Text(this, SWT.BORDER);
		txtXName.setToolTipText("最长64位");
		txtXName.setTextLimit(64);
		txtXName.setBounds(358, 167, 200, 30);
		
		lblField = new Label(this, SWT.NONE);
		lblField.setText("字段名1");
		lblField.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblField.setAlignment(SWT.RIGHT);
		lblField.setBounds(272, 207, 80, 22);
		
		txtXField = new Text(this, SWT.BORDER);
		txtXField.setToolTipText("最长64位");
		txtXField.setTextLimit(64);
		txtXField.setBounds(358, 203, 200, 30);
		
		lblField2 = new Label(this, SWT.NONE);
		lblField2.setText("字段名2");
		lblField2.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblField2.setAlignment(SWT.RIGHT);
		lblField2.setBounds(272, 243, 80, 22);
		
		txtXField2 = new Text(this, SWT.BORDER);
		txtXField2.setToolTipText("最长64位");
		txtXField2.setTextLimit(64);
		txtXField2.setBounds(358, 239, 200, 30);
		
		lblFIeld3 = new Label(this, SWT.NONE);
		lblFIeld3.setText("字段名3");
		lblFIeld3.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblFIeld3.setAlignment(SWT.RIGHT);
		lblFIeld3.setBounds(272, 279, 80, 22);
		
		txtXField3 = new Text(this, SWT.BORDER);
		txtXField3.setToolTipText("最长64位");
		txtXField3.setTextLimit(64);
		txtXField3.setBounds(358, 275, 200, 30);
		
		lblNote = new Label(this, SWT.NONE);
		lblNote.setText("备注");
		lblNote.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblNote.setAlignment(SWT.RIGHT);
		lblNote.setBounds(272, 315, 80, 22);
		
		txtXNote = new Text(this, SWT.BORDER);
		txtXNote.setToolTipText("最长65535");
		txtXNote.setBounds(358, 311, 200, 30);
		
		lblStatus = new Label(this, SWT.NONE);
		lblStatus.setText("状态");
		lblStatus.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		lblStatus.setAlignment(SWT.RIGHT);
		lblStatus.setBounds(272, 351, 80, 22);
		
		//cmbStatus = new Combo(this, SWT.NONE);
		cmbStatus = new WStatusCombo(this, SWT.BORDER);
		cmbStatus.setBounds(358, 347, 200, 34);
		
		btnSave = new Button(this, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.save();
			}
		});
		btnSave.setSelection(true);
		btnSave.setBounds(564, 387, 87, 34);
		btnSave.setText("保存");
		
		btnReset = new Button(this, SWT.NONE);
		btnReset.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//Composite parent = self.getParent();
				//self.dispose();
				//self = new DicAddShelf();
				//new BaseDialog(self, new DicAddShell()).open();
				self.init();
			}
		});
		btnReset.setText("重置");
		btnReset.setSelection(true);
		btnReset.setBounds(471, 387, 87, 34);
		this.createContent();
		this.init();
	}
	
	
	
	protected void init() {
		txtSID.setText(dda.getIDService.yp_getid(0, "yt_dic", 1).toString());
		txtXName.setText("");
		txtXField.setText("");
		txtXField2.setText("");
		txtXField3.setText("");
		txtXNote.setText("");
		cmbStatus.select(1);
		txtXName.setFocus();
	}
	
	private void createContent() {
		this.setSize(800, 600);
		ShellUtil.center(self);
	}
	
	protected void save() {
		if(checkInput()) {
			Integer sID = Integer.valueOf(txtSID.getText());
			if(dda.dicService.findByID(sID) != null) {
				this.error("sID重复");
				txtSID.setFocus();
				return;
			}
			YTDic dic = new YTDic().setSID(sID).setXName(txtXName.getText()).setXField(txtXField.getText()).setXField2(txtXField2.getText())
					.setXField3(txtXField3.getText()).setXNote(txtXNote.getText()).setSStatus(Integer.valueOf(String.valueOf(cmbStatus.getText().charAt(0))))
					;
			if(dda.dicService.save(dic)) {
				Msg.show(self, "保存", "保存成功");
				self.dispose();
			}else {
				this.error("保存失败");
			}
		}
	}
	
	protected boolean checkInput() {
		boolean rs = true;
		// sID, xName, sStatus
		//NumberUtil.
		if(!this.isNumber(txtSID.getText(), 0)) {
			this.error("sID错误");
			txtSID.setFocus();
			return rs;
		}
		if(txtXName.getText().length() == 0) {
			this.error("名称不能为空");
			txtXName.setFocus();
			return rs;
		}
		return rs;
	}
	
	protected void error(String msg) {
		Msg.show(self, "错误", msg);
	}
	
	/**
	 * 检查字符串是否数值
	 * @param str 字符串
 	 * @param style 0整数, 1带小数点的数而且最多只有一个
	 * @return
	 */
	protected boolean isNumber(String str, int style) {
		boolean rs = true;
		if(str == null || str.length() == 0) {
			rs = false;
		}else {
			for(char c : str.toCharArray()) {
				if((style == 0 && (c < 48 || c > 57)) || (style == 1 && (c < 48 || c > 57) && c != 46)) {
					rs = false;
					break;
				}
			}
		}
		return rs;
				
	}

	@Override
	protected String getTitle() {
		return "新增";
	}
}
